﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker.CustomControls
{
    public partial class Filter : Form
    {
        public issueList iList = null;
        public Filter()
        {
            InitializeComponent();
        }

        private void Filter_Load(object sender, EventArgs e)
        {
            cmbPriority.DataSource = Service.NewIssue.GetPriorityOptions();
            cmbPriority.DisplayMember = "Name";
            cmbPriority.ValueMember = "ID";

            cmbStatus.DataSource = Service.NewIssue.GetStatusOptions();
            cmbStatus.DisplayMember = "Name";
            cmbStatus.ValueMember = "ID";

            cmbAssignToOptions.SelectedIndex = 0;

            //load filter
            if (iList.doFilter.Priority.HasValue)
            {
                chkPriority.Checked = true;
                cmbPriority.SelectedValue = (int)iList.doFilter.Priority.Value;
            }
            if (iList.doFilter.Status.HasValue)
            {
                chkStatus.Checked = true;
                cmbStatus.SelectedValue = (int)iList.doFilter.Status.Value;
            }
            if (iList.doFilter.From.HasValue)
            {
                dateTimePickerFrom.Checked = true;
                dateTimePickerFrom.Value = iList.doFilter.From.Value;
            }
            if (iList.doFilter.To.HasValue)
            {
                dateTimePickerTo.Checked = true;
                dateTimePickerTo.Value = iList.doFilter.To.Value;
            }
            if (!string.IsNullOrEmpty(iList.doFilter.SelectedAssigneeOption))
            {
                chkAssignee.Checked = true;
                cmbAssignToOptions.Text = iList.doFilter.SelectedAssigneeOption;
            }
            if (iList.doFilter.Shops != null)
                cmbAssignTo.Text = iList.doFilter.SelectedOption;

            //disable assignee for user
            if (ActiveSession.User.Role != Role.Admininstrator)
                chkAssignee.Enabled = false;            
            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (chkPriority.Checked)
                iList.doFilter.Priority = (Priority)cmbPriority.SelectedValue;
            else
                iList.doFilter.Priority = null;
            if (chkStatus.Checked)
                iList.doFilter.Status = (Status)cmbStatus.SelectedValue;
            else
                iList.doFilter.Status = null;
            if (dateTimePickerFrom.Checked)
                iList.doFilter.From = dateTimePickerFrom.Value;
            else
                iList.doFilter.From = null;
            if (dateTimePickerTo.Checked)
                iList.doFilter.To = dateTimePickerTo.Value;
            else
                iList.doFilter.To = null;
            if (chkAssignee.Checked)
            {
                List<Shop> assignee = new List<Shop>();
                switch (cmbAssignToOptions.Text)
                {
                    case "All Shops":
                        //assignee = Service.Admin.GetShops();
                        break;
                    case "Area":
                        assignee = Service.Admin.GetShops(areaCode: cmbAssignTo.SelectedValue.ToString());
                        iList.doFilter.SelectedOption = cmbAssignTo.Text;
                        break;
                    case "Group":
                        assignee = Service.Admin.GetShops(groupId: Convert.ToInt32(cmbAssignTo.SelectedValue));
                        iList.doFilter.SelectedOption = cmbAssignTo.Text;
                        break;
                    case "Shop":
                        Shop obj = new Shop();
                        obj.ID = Convert.ToInt32(cmbAssignTo.SelectedValue);
                        assignee.Add(obj);
                        iList.doFilter.SelectedOption = cmbAssignTo.Text;
                        break;
                    default:
                        break;
                }
                iList.doFilter.Shops = assignee.Select(x => x.ID).ToList();
                iList.doFilter.SelectedAssigneeOption = cmbAssignToOptions.Text;                

            }
            else
            {
                iList.doFilter.Shops = null;
                iList.doFilter.SelectedAssigneeOption = string.Empty;
                iList.doFilter.SelectedOption = string.Empty;
            }

            bool chk = false;
            if (chkPriority.Checked
                || chkStatus.Checked
                || dateTimePickerFrom.Checked
                || dateTimePickerTo.Checked
                || chkAssignee.Checked)
                chk = true;
            iList.SetFilterBGImage(chk);
            this.Close();
        }

        private void cmbAssignToOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            cmbAssignTo.Visible = false;
            switch (cmbAssignToOptions.Text)
            {
                case "All Shops":
                    break;
                case "Area":
                    cmbAssignTo.Visible = true;
                    cmbAssignTo.DataSource = Service.Admin.GetAreas();
                    cmbAssignTo.DisplayMember = "Name";
                    cmbAssignTo.ValueMember = "AreaCode";
                    break;
                case "Group":
                    cmbAssignTo.Visible = true;
                    cmbAssignTo.DataSource = Service.Admin.GetGroups();
                    cmbAssignTo.DisplayMember = "Name";
                    cmbAssignTo.ValueMember = "ID";
                    break;
                case "Shop":
                    cmbAssignTo.Visible = true;
                    cmbAssignTo.DataSource = Service.Admin.GetShops();
                    cmbAssignTo.DisplayMember = "Name";
                    cmbAssignTo.ValueMember = "ID";
                    break;
                default:
                    break;
            }
            Cursor.Current = Cursors.Default;
        }

        private void chkPriority_CheckedChanged(object sender, EventArgs e)
        {
            cmbPriority.Enabled = false;
            if (chkPriority.Checked)
                cmbPriority.Enabled = true;
        }

        private void chkStatus_CheckedChanged(object sender, EventArgs e)
        {
            cmbStatus.Enabled = false;
            if (chkStatus.Checked)
                cmbStatus.Enabled = true;
        }

        private void chkAssignee_CheckedChanged(object sender, EventArgs e)
        {
            cmbAssignToOptions.Enabled = false;
            if (chkAssignee.Checked)
                cmbAssignToOptions.Enabled = true;
        }


    }
}
